class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        if not head:
            return None
        now_note, length = head, 1
        while now_note.next:
            now_note = now_note.next
            length += 1
        if length == 1:
            return head
        k %= length
        if not k:
            return head
        now_note, index, end_note = head, 1, now_note
        while index < length - k:
            index += 1
            now_note = now_note.next
        head, now_note.next, end_note.next = now_note.next, None, head
        return head


lists = [5, 4, 3, 2, 1]
last_note = None
for num in lists:
    now_note = ListNode(num)
    now_note.next, last_note = last_note, now_note
a = Solution()
print(a.rotateRight(now_note, 2).next.val)